/*
 * SimpleLogger.java  0.4.0 / Oct 23, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.logging.simple;

import java.io.OutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.cute4j.logging.Logger;
import com.cute4j.logging.LoggingAdapter;

/**
 * Simple logging adapter implementation.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class SimpleLogger implements LoggingAdapter {

// Private Static Fields. ------------------------------------------------------

  private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

// Private Fields. -------------------------------------------------------------

  private final PrintStream errorStream;
  private final PrintStream infoStream;
  private final SimpleDateFormat dateFormater;
  private int level;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new SimpleLoggingAdapter object.
   */
  public SimpleLogger() {
    this(System.err);
  }

  /**
   * Constructs new SimpleLoggingAdapter object.
   *
   * @param infoStream the info stream
   */
  public SimpleLogger(OutputStream infoStream) {
    this(new PrintStream(infoStream));
  }

  /**
   * Constructs new SimpleLoggingAdapter object.
   *
   * @param errorStream the error stream
   * @param infoStream the info stream
   */
  public SimpleLogger(OutputStream errorStream, OutputStream infoStream) {
    this(new PrintStream(infoStream), new PrintStream(errorStream));
  }

  /**
   * Constructs new SimpleLoggingAdapter object.
   *
   * @param errorStream the error print stream
   */
  public SimpleLogger(PrintStream errorStream) {
    this(errorStream, errorStream);
  }

  /**
   * Constructs new SimpleLoggingAdapter object.
   *
   * @param errorStream the error print stream
   * @param infoStream the info print stream
   */
  public SimpleLogger(PrintStream errorStream, PrintStream infoStream) {
    if (errorStream == null) {
      throw new IllegalArgumentException("Null errorStream");
    }
    this.errorStream = errorStream;
    if (infoStream == null) {
      this.infoStream = errorStream;
    } else {
      this.infoStream = infoStream;
    }
    dateFormater = new SimpleDateFormat(DATE_FORMAT); // NOPMD - default locale
  }

// LoggingAdapter Interface Implementation. ------------------------------------

  /**
   * {@inheritDoc}
   */
  public String getName() {
    return "Simple Logger";
  }

  /**
   * {@inheritDoc}
   */
  public boolean isLoggable(int level) {
    return (this.level <= level);
  }

  /**
   * {@inheritDoc}
   */
  public int getLevel() {
    return level;
  }

  /**
   * {@inheritDoc}
   */
  public void setLevel(int level) {
    if ((level < Logger.LEVEL_INFO) || (level > Logger.LEVEL_ERROR)) {
      throw new IllegalArgumentException("Invalid level");
    }
    this.level = level;
  }

  /**
   * {@inheritDoc}
   */
  public void log(int level, String message, Throwable t) {
    if (level >= this.level) {
      final int logMethodIndex = 2;
      StringBuffer location = new StringBuffer();
      Throwable traceException = new Throwable(); // NOPMD - fake exception
      StackTraceElement[] stackTrace = traceException.getStackTrace();
      if ((stackTrace != null) && (stackTrace.length > logMethodIndex)) {
        location.append(stackTrace[logMethodIndex]);
      }
      log(level, location.toString(), message, t);
    }
  }

  /**
   * {@inheritDoc}
   */
  public void log(int level, String fileName, int lineNumber, String message,
                  Throwable t) {
    if (level >= this.level) {
      StringBuffer location = new StringBuffer();
      if (fileName != null) {
        location.append(fileName);
        if (lineNumber >= 0) { // NOPMD
          location.append(':');
          location.append(lineNumber);
        }
      }
      log(level, location.toString(), message, t);
    }
  }

// Private Methods. ------------------------------------------------------------

  private void log(int level, String location, String message, Throwable t) {
    PrintStream ps;
    if (level == Logger.LEVEL_INFO) {
      ps = infoStream;
    } else {
      ps = errorStream;
    }
    ps.print(dateFormater.format(new Date()));
    ps.print(" ");
    switch (level) {
      case Logger.LEVEL_INFO:
        ps.print("INFO ");
        break;
      case Logger.LEVEL_WARN:
        ps.print("WARN ");
        break;
      default:
        ps.print("ERROR ");
        break;
    }
    if ((location != null) && (location.length() > 0)) {
      ps.print(location);
    }
    if (message == null) {
      ps.println();
    } else {
      ps.print(": ");
      ps.println(message);
    }
    if (t != null) {
      t.printStackTrace(ps);
    }
  }

}
